package org.jim.dmitrykalachov;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class MenuDispatcher {

    private static String EXIT = "exit";
    private final IHandler iHandler;

    public MenuDispatcher(IHandler iHandler) {
        this.iHandler = iHandler;
    }

    public void run() {

        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            String line = null;
            while ((line = br.readLine()) != null) {
                if (EXIT.equals(line)) {
                    break;
                }
                String output = iHandler.handle(line);
                if (output != null) {
                    System.out.println(output);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
